#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
部署示例
演示了如何配置可部署的应用
"""

from fastapi import FastAPI
from pydantic import BaseModel
from typing import Union
import os

app = FastAPI(
    title="FastAPI 学习示例",
    description="这是一个用于演示 FastAPI 各种功能的示例应用",
    version="1.0.0"
)

class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None

# 环境变量配置
PORT = int(os.environ.get("PORT", 8000))
HOST = os.environ.get("HOST", "127.0.0.1")

@app.get("/")
async def read_root():
    return {"message": "Hello World from Docker!"}

@app.get("/items/{item_id}")
async def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}

@app.post("/items/")
async def create_item(item: Item):
    return item

@app.get("/health")
async def health_check():
    return {"status": "healthy"}

# 用于 Gunicorn 的应用实例
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        "docker_example:app", 
        host=HOST, 
        port=PORT, 
        log_level="info",
        reload=True  # 开发环境使用，生产环境应设为 False
    )